package br.com.Orange.view.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import br.com.Orange.negocio.Fachada;
import br.com.Orange.negocio.funcionario.exception.FuncionarioInexistenteException;
import br.com.Orange.negocio.funcionario.model.Funcionario;

public class LoginForm extends ActionForm {

	private static final long serialVersionUID = 5516217177274544221L;
	
	private Funcionario funcionario = new Funcionario();
	
	public Funcionario getFuncionario(){
		return this.funcionario;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors erros = new ActionErrors();
		
		try {
			String senha = Fachada.obterInstancia().procurarFuncionario(funcionario.getMatricula()).getSenha();
			if (!funcionario.getSenha().trim().equalsIgnoreCase(senha)){
				erros.add(("senha"), new ActionMessage("erro.login.senhaErrada"));
			}
		} catch (FuncionarioInexistenteException e) {
			erros.add(("matricula"), new ActionMessage("erro.login.funcionarioInexistente"));
		}
		
		
		return erros;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.funcionario = new Funcionario();
	}
	
}
